package com.honeycomb.common;

import org.apache.commons.lang3.StringEscapeUtils;

import com.honeycomb.utils.SystemValidate;

public class ParamConverter {
	
	public static int convertToInteger(String intStr) {
		try {
			return Integer.parseInt(intStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to int type, input=" + intStr, ex);
		}
	}
	
	public static Long convertToLongObj(String longStr) {
		try {
			return Long.decode(longStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to Long type, input=" + longStr, ex);
		}
	}
	
	public static double convertToDouble(String doubleStr) {
		try {
			return Double.parseDouble(doubleStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to double type, input=" + doubleStr, ex);
		}
	}
	
	// 对html相关字符进行转义
	public static String convertToHtmlCapableString(String text) {
		return StringEscapeUtils.escapeHtml4(text);
	}
	
	public static String assertMaxLength(String str, int length) {
		if (str.length() > length) {
			throw new ValidateFailException("Input string " + (str.length() > 15 ? str.substring(0, 15) + "..." : str) + "already exceeded the length limit " + length); 
		}
		else {
			return str;
		}
	}
	
	public static boolean isNullOrEmpty(String str) {
		if (str == null || str.isEmpty() ) {
			return true;
		}
		else {
			return false;
		}
	}

	public static String assertNotEmpty(String str) {
		if (isNullOrEmpty(str)) {
			throw new ValidateFailException("input string is null or empty");
		}
		else {
			return str;
		}
	}
	
	public static String assertNotNull(String str) {
		if (str == null) {
			throw new ValidateFailException("input string is null");
		}
		else {
			return str;
		}
	}
	
	public static String assertEmailString(String str) {
		if (!SystemValidate.validateInput(str, "E")) {
			throw new ValidateFailException("input string is not legal e-mail string:" + str);
		}
		else {
			return str;
		}
	}

}
